<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../plugin/layui-v2.2.5/layui/css/layui.css">
</head>
<body>
<div align="center">
	<fieldset class="layui-elem-field">
      <legend>个人信息</legend>
      <div class="layui-field-box" style="height: 200px">	
        <table class="layui-table" id="teacher-info" style="width: 60%;">
          <tr>
          	<td rowspan="3" style="width:100px;">
          		<div style="width:100px;float:right" align="center">
			        <img src="http://t.cn/RCzsdCq" class="layui-upload-img" id="personalImg" style="width: 100px;height: 100px;display:block" >
			        <button type="button" class="layui-btn layui-btn-xs" id="uploadImg" >更改头像</button>      		
		      	</div>
          	</td>
            <td>工号</td>
            <td id="personal_workId"></td>
          </tr>
          <tr>
            <td>姓名</td>
            <td id="personal_password"></td>
          </tr>
          <tr>
            <td>学院</td>
            <td id="personal_academe"></td>
          </tr>
        </table>
      </div>
      	<legend>修改密码</legend><hr>
      	<div style="width: 70%">
	      	<div class="layui-row">
		        <div class="layui-form-item">
		          <label class="layui-form-label">输入旧密码</label>
		          <div class="layui-input-block">
		            <input type="password" name="oldPassword" id="oldPassword" class="layui-input" type="text" autocomplete="off" lay-verify="title">  
		          </div>
		        </div>
		    </div>
		    <div class="layui-row">
		        <div class="layui-form-item">
		          <label class="layui-form-label">输入新密码</label>
		          <div class="layui-input-block">
		            <input name="newPassword" id="newPassword" class="layui-input" type="text" autocomplete="off" lay-verify="title">
		          </div>
		        </div>
		    </div>   
		    <div class="layui-row">
		        <button class="layui-btn" id="modifyPassword">立即修改</button>
		    </div>    		
      	</div>
    </fieldset>
</div>

<script src="../plugin/layui-v2.2.5/layui/layui.js"></script>
<script src="../plugin/jquery-3.1.1.min.js"></script>
<script>
//定义常量记录教师id用于更改密码

var con = 0; 

layui.use(['layer','upload'], function(){
  //var form = layui.form;
  var $ = layui.jquery
  ,upload = layui.upload;
  
  //加载教师数据
  $.ajax({ 
	url: "/CMA/teacher/getTeacherById",
	contentType: "application/x-www-form-urlencoded",
	type:"post",
	dataType : "json",
	success: function(result){
		console.log("SS");
		console.log(result.data);
		var teacher = result.data;
		con = teacher.teacherid;
		document.getElementById("personal_workId").innerHTML=teacher.workid;
		document.getElementById("personal_password").innerHTML=teacher.name;
		document.getElementById("personal_academe").innerHTML=teacher.academeName;
      }
  });
  
  var uploadInst = upload.render({
	    elem: '#uploadImg'
	    ,url: '/CMA/teacher/uploadImg'
	    ,before: function(obj){
	      //预读本地文件示例，不支持ie8
	      obj.preview(function(index, file, result){
	        $('#personalImg').attr('src', result); //图片链接（base64）
	      });
	    }
	    ,done: function(res){
	      //如果上传失败
	      if(res.code > 0){
	        return layer.msg('上传失败');
	      }
	      //上传成功
	      
	    }
	    ,error: function(){
	      //演示失败状态，并实现重传
	      var demoText = $('#demoText');
	      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
	      demoText.find('.demo-reload').on('click', function(){
	        uploadInst.upload();
	      });
	    }
	  });
  
  $("#modifyPassword").on("click",function(){
	  var oldPwd = $("#oldPassword").val();
	  var newPwd = $("#newPassword").val();
	  //var workid = 
	  console.log(oldPwd+""+newPwd);
	  $.ajax({ 
      	url: "/CMA/teacher/modifyPassword",
      	contentType: "application/x-www-form-urlencoded",
      	type:"post",
      	dataType : "json",
      	data : {
      		id : con,
      		oldPassword : oldPwd,
      		newPassword : newPwd
      	},
      	success: function(data){
      		alert(data.msg);
      		
      	},
      	error : function(){
      		alert("error");
      	}
        });
  })
  
});
</script>
</body>
</html>